/*
 * Copyright © 2011 Nokia Corporation. All rights reserved.
 * Nokia and Nokia Connecting People are registered trademarks of Nokia Corporation. 
 * Oracle and Java are trademarks or registered trademarks of Oracle and/or its
 * affiliates. Other product and company names mentioned herein may be trademarks
 * or trade names of their respective owners. 
 * See LICENSE.TXT for license information.
 */ 

package com.nokia.example.wordpress.components;

import com.nokia.example.wordpress.networking.Blog;
import com.nokia.example.wordpress.networking.Comment;
import com.nokia.example.wordpress.networking.Post;
import java.util.Vector;
import javax.microedition.rms.RecordStore;

/**
 * Contains the data used in the application:
 * blog, posts, comments, user credentials.
 */
public class DataModel {

    private static DataModel self = null;
    /**
     * The main blog.
     */
    private Blog blog;
    /**
     * Blog posts
     */
    private Vector posts;
    /**
     * Currently selected post.
     * Index in the above array.
     */
    private int currentPostIndex;
    /**
     * Comments in the blog.
     */
    private Vector comments;
    /**
     * Currently selected comment.
     * Index in the above array.
     */
    private int currentCommentIndex;
    /**
     * Comments for the currently selected post.
     */
    private Vector commentsForCurrentPost;
    /**
     * Currently selected comment, index in the above array.
     */
    private int commentsForCurrentPostIndex;
    /**
     * Record id
     */
    private static final int RECORD_URL = 1;
    /**
     * Record id
     */
    private static final int RECORD_USERNAME = 2;
    /**
     * Record id
     */
    private static final int RECORD_PASSWORD = 3;
    /**
     * User blog url
     */
    private String loginUrl = "http://<blogname>.wordpress.com/xmlrpc.php";
    /**
     * Login user name
     */
    private String loginUsername = "";
    /**
     * Login password
     */
    private String loginPassword = "";
    /**
     * Login message. This will contain an error message received from
     * the server during the login process. Empty if all went ok.
     */
    private String loginMessage = "";

    public DataModel() {
        loadLoginCredentials();
    }

    public static DataModel getInstance() {
        if (self == null) {
            self = new DataModel();
        }
        return self;
    }

    public void setBlog(Blog blog) {
        this.blog = blog;
    }

    public Blog getBlog() {
        return blog;
    }

    public void setComments(Vector comments) {
        this.comments = comments;
    }

    public void setPosts(Vector posts) {
        this.posts = posts;
    }

    public Vector getComments() {
        return comments;
    }

    public Vector getPosts() {
        return posts;
    }

    public Vector getCommentsForCurrentPost() {
        populateCommentsForCurrentPost();
        return commentsForCurrentPost;
    }

    /**
     * Fetch the comments for the post that was selected.
     * This is used for button labels and comments view.
     */
    private void populateCommentsForCurrentPost() {
        commentsForCurrentPost = new Vector();
        Post selectedPost = getCurrentPost();
        for (int i = 0; i < comments.size(); i++) {
            Comment comment = (Comment) comments.elementAt(i);
            if (comment.postId.equals(selectedPost.postId)) {
                commentsForCurrentPost.addElement(comment);
            }
        }
    }

    public void setCurrentPostIndex(int index) {
        this.currentPostIndex = index;
    }

    public int getCurrentPostIndex() {
        return currentPostIndex;
    }

    public Post getCurrentPost() {
        return (Post) posts.elementAt(currentPostIndex);
    }

    public void setCurrentCommentIndex(int index) {
        this.currentCommentIndex = index;
    }

    public int getCurrentCommentIndex() {
        return currentCommentIndex;
    }

    public Comment getCurrentComment() {
        return (Comment) comments.elementAt(currentCommentIndex);
    }

    public void setCommentForCurrentPostIndex(int index) {
        this.commentsForCurrentPostIndex = index;
    }

    public int getCommentForCurrentPostIndex() {
        return commentsForCurrentPostIndex;
    }

    public Comment getCurrentCommentForPost() {
        return (Comment) commentsForCurrentPost.elementAt(commentsForCurrentPostIndex);
    }

    public String getBlogUrl() {
        return loginUrl;
    }

    public void setBlogUrl(String blogUrl) {
        this.loginUrl = blogUrl;
    }

    public String getLoginPassword() {
        return loginPassword;
    }

    public void setLoginPassword(String loginPassword) {
        this.loginPassword = loginPassword;
    }

    public String getLoginUsername() {
        return loginUsername;
    }

    public void setLoginUsername(String loginUsername) {
        this.loginUsername = loginUsername;
    }

    public String getLoginMessage() {
        return loginMessage;
    }

    public void setLoginMessage(String loginMessage) {
        this.loginMessage = loginMessage;
    }

    /**
     * Load login credentials from the record store.
     */
    public void loadLoginCredentials() {
        RecordStore store = null;
        try {
            //RecordStore.deleteRecordStore("wordpress");
            store = RecordStore.openRecordStore("wordpress", true);

            if (store.getNextRecordID() == 1) {
                System.out.println("init");
                byte[] bytes = loginUrl.getBytes();
                store.addRecord(bytes, 0, bytes.length);
                bytes = loginUsername.getBytes();
                store.addRecord(bytes, 0, bytes.length);
                bytes = loginPassword.getBytes();
                store.addRecord(bytes, 0, bytes.length);
            }

            byte[] buffer = new byte[256];
            int length = store.getRecord(RECORD_URL, buffer, 0);
            String string = new String(buffer, 0, length);
            loginUrl = string;

            length = store.getRecord(RECORD_USERNAME, buffer, 0);
            string = new String(buffer, 0, length);
            loginUsername = string;

            length = store.getRecord(RECORD_PASSWORD, buffer, 0);
            string = new String(buffer, 0, length);
            loginPassword = string;

        } catch (javax.microedition.rms.RecordStoreException ex) {
            System.out.println("loadSettings " + ex.getMessage());
        } finally {
            if (store != null) {
                try {
                    store.closeRecordStore();
                } catch (Exception ex) {
                }
            }
        }
    }

    /**
     * Save login credentials the record store.
     */
    public void saveLoginCredentials() {
        RecordStore store = null;
        try {
            store = RecordStore.openRecordStore("wordpress", true);
            byte[] bytes = loginUrl.getBytes();
            store.setRecord(RECORD_URL, bytes, 0, bytes.length);

            bytes = loginUsername.getBytes();
            store.setRecord(RECORD_USERNAME, bytes, 0, bytes.length);

            bytes = loginPassword.getBytes();
            store.setRecord(RECORD_PASSWORD, bytes, 0, bytes.length);

        } catch (javax.microedition.rms.RecordStoreException ex) {
            System.out.println("saveSettings " + ex.getMessage());
        } finally {
            if (store != null) {
                try {
                    store.closeRecordStore();
                } catch (Exception ex) {
                }
            }
        }
    }
}
